{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.11 \u7528Cython\u5199\u9ad8\u6027\u80fd\u7684\u6570\u7ec4\u64cd\u4f5c\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8981\u5199\u9ad8\u6027\u80fd\u7684\u64cd\u4f5c\u6765\u81eaNumPy\u4e4b\u7c7b\u7684\u6570\u7ec4\u8ba1\u7b97\u51fd\u6570\u3002\n\u4f60\u5df2\u7ecf\u77e5\u9053\u4e86Cython\u8fd9\u6837\u7684\u5de5\u5177\u4f1a\u8ba9\u5b83\u53d8\u5f97\u7b80\u5355\uff0c\u4f46\u662f\u5e76\u4e0d\u786e\u5b9a\u8be5\u600e\u6837\u53bb\u505a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u4e00\u4e2aCython\u51fd\u6570\uff0c\u7528\u6765\u4fee\u6574\u4e00\u4e2a\u7b80\u5355\u7684\u4e00\u7ef4\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u503c\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# sample.pyx (Cython)\n\ncimport cython\n\n@cython.boundscheck(False)\n@cython.wraparound(False)\ncpdef clip(double[:] a, double min, double max, double[:] out):\n    '''\n    Clip the values in a to be between min and max. Result in out\n    '''\n    if min > max:\n        raise ValueError(\"min must be <= max\")\n    if a.shape[0] != out.shape[0]:\n        raise ValueError(\"input and output arrays must be the same size\")\n    for i in range(a.shape[0]):\n        if a[i] < min:\n            out[i] = min\n        elif a[i] > max:\n            out[i] = max\n        else:\n            out[i] = a[i]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u7f16\u8bd1\u548c\u6784\u5efa\u8fd9\u4e2a\u6269\u5c55\uff0c\u4f60\u9700\u8981\u4e00\u4e2a\u50cf\u4e0b\u9762\u8fd9\u6837\u7684 setup.py \u6587\u4ef6\n\uff08\u4f7f\u7528 python3 setup.py build_ext --inplace \u6765\u6784\u5efa\u5b83\uff09\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from distutils.core import setup\nfrom distutils.extension import Extension\nfrom Cython.Distutils import build_ext\n\next_modules = [\n    Extension('sample',\n              ['sample.pyx'])\n]\n\nsetup(\n  name = 'Sample app',\n  cmdclass = {'build_ext': build_ext},\n  ext_modules = ext_modules\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u4f1a\u53d1\u73b0\u7ed3\u679c\u51fd\u6570\u786e\u5b9e\u5bf9\u6570\u7ec4\u8fdb\u884c\u7684\u4fee\u6b63\uff0c\u5e76\u4e14\u53ef\u4ee5\u9002\u7528\u4e8e\u591a\u79cd\u7c7b\u578b\u7684\u6570\u7ec4\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# array module example\nimport sample\nimport array\na = array.array('d',[1,-3,4,7,2,0])\na"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.clip(a,1,4,a)\na"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# numpy example\nimport numpy\nb = numpy.random.uniform(-10,10,size=1000000)\nb"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = numpy.zeros_like(b)\nc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.clip(b,-5,5,c)\nc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "min(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "max(c)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fd8\u4f1a\u53d1\u73b0\u8fd0\u884c\u751f\u6210\u7ed3\u679c\u975e\u5e38\u7684\u5feb\u3002\n\u4e0b\u9762\u6211\u4eec\u5c06\u672c\u4f8b\u548cnumpy\u4e2d\u7684\u5df2\u5b58\u5728\u7684 clip() \u51fd\u6570\u505a\u4e00\u4e2a\u6027\u80fd\u5bf9\u6bd4\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "timeit('numpy.clip(b,-5,5,c)','from __main__ import b,c,numpy',number=1000)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "timeit('sample.clip(b,-5,5,c)','from __main__ import b,c,sample',\n        number=1000)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u770b\u5230\u7684\uff0c\u5b83\u8981\u5feb\u5f88\u591a\u2014\u2014\u8fd9\u662f\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u7ed3\u679c\uff0c\u56e0\u4e3aNumPy\u7248\u672c\u7684\u6838\u5fc3\u4ee3\u7801\u8fd8\u662f\u7528C\u8bed\u8a00\u5199\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u5229\u7528\u4e86Cython\u7c7b\u578b\u7684\u5185\u5b58\u89c6\u56fe\uff0c\u6781\u5927\u7684\u7b80\u5316\u4e86\u6570\u7ec4\u7684\u64cd\u4f5c\u3002\ncpdef clip() \u58f0\u660e\u4e86 clip() \u540c\u65f6\u4e3aC\u7ea7\u522b\u51fd\u6570\u4ee5\u53caPython\u7ea7\u522b\u51fd\u6570\u3002\n\u5728Cython\u4e2d\uff0c\u8fd9\u4e2a\u662f\u5f88\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u8868\u793a\u6b64\u51fd\u6570\u8c03\u7528\u8981\u6bd4\u5176\u4ed6Cython\u51fd\u6570\u66f4\u52a0\u9ad8\u6548\n\uff08\u6bd4\u5982\u4f60\u60f3\u5728\u53e6\u5916\u4e00\u4e2a\u4e0d\u540c\u7684Cython\u51fd\u6570\u4e2d\u8c03\u7528clip()\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u578b\u53c2\u6570 double[:] a \u548c double[:] out \u58f0\u660e\u8fd9\u4e9b\u53c2\u6570\u4e3a\u4e00\u7ef4\u7684\u53cc\u7cbe\u5ea6\u6570\u7ec4\u3002\n\u4f5c\u4e3a\u8f93\u5165\uff0c\u5b83\u4eec\u4f1a\u8bbf\u95ee\u4efb\u4f55\u5b9e\u73b0\u4e86\u5185\u5b58\u89c6\u56fe\u63a5\u53e3\u7684\u6570\u7ec4\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5728PEP 3118\u6709\u8be6\u7ec6\u5b9a\u4e49\u3002\n\u5305\u62ec\u4e86NumPy\u4e2d\u7684\u6570\u7ec4\u548c\u5185\u7f6e\u7684array\u5e93\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u7f16\u5199\u751f\u6210\u7ed3\u679c\u4e3a\u6570\u7ec4\u7684\u4ee3\u7801\u65f6\uff0c\u4f60\u5e94\u8be5\u9075\u5faa\u4e0a\u9762\u793a\u4f8b\u90a3\u6837\u8bbe\u7f6e\u4e00\u4e2a\u8f93\u51fa\u53c2\u6570\u3002\n\u5b83\u4f1a\u5c06\u521b\u5efa\u8f93\u51fa\u6570\u7ec4\u7684\u8d23\u4efb\u7ed9\u8c03\u7528\u8005\uff0c\u4e0d\u9700\u8981\u77e5\u9053\u4f60\u64cd\u4f5c\u7684\u6570\u7ec4\u7684\u5177\u4f53\u7ec6\u8282\n\uff08\u5b83\u4ec5\u4ec5\u5047\u8bbe\u6570\u7ec4\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\uff0c\u53ea\u9700\u8981\u505a\u4e00\u4e9b\u5c0f\u7684\u68c0\u67e5\u6bd4\u5982\u786e\u4fdd\u6570\u7ec4\u5927\u5c0f\u662f\u6b63\u786e\u7684\uff09\u3002\n\u5728\u50cfNumPy\u4e4b\u7c7b\u7684\u5e93\u4e2d\uff0c\u4f7f\u7528 numpy.zeros() \u6216 numpy.zeros_like()\n\u521b\u5efa\u8f93\u51fa\u6570\u7ec4\u76f8\u5bf9\u800c\u8a00\u6bd4\u8f83\u5bb9\u6613\u3002\u53e6\u5916\uff0c\u8981\u521b\u5efa\u672a\u521d\u59cb\u5316\u6570\u7ec4\uff0c\n\u4f60\u53ef\u4ee5\u4f7f\u7528 numpy.empty() \u6216 numpy.empty_like() .\n\u5982\u679c\u4f60\u60f3\u8986\u76d6\u6570\u7ec4\u5185\u5bb9\u4f5c\u4e3a\u7ed3\u679c\u7684\u8bdd\u9009\u62e9\u8fd9\u4e24\u4e2a\u4f1a\u6bd4\u8f83\u5feb\u70b9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f60\u7684\u51fd\u6570\u5b9e\u73b0\u4e2d\uff0c\u4f60\u53ea\u9700\u8981\u7b80\u5355\u7684\u901a\u8fc7\u4e0b\u6807\u8fd0\u7b97\u548c\u6570\u7ec4\u67e5\u627e\uff08\u6bd4\u5982a[i],out[i]\u7b49\uff09\u6765\u7f16\u5199\u4ee3\u7801\u64cd\u4f5c\u6570\u7ec4\u3002\nCython\u4f1a\u8d1f\u8d23\u4e3a\u4f60\u751f\u6210\u9ad8\u6548\u7684\u4ee3\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "clip() \u5b9a\u4e49\u4e4b\u524d\u7684\u4e24\u4e2a\u88c5\u9970\u5668\u53ef\u4ee5\u4f18\u5316\u4e0b\u6027\u80fd\u3002\n@cython.boundscheck(False) \u7701\u53bb\u4e86\u6240\u6709\u7684\u6570\u7ec4\u8d8a\u754c\u68c0\u67e5\uff0c\n\u5f53\u4f60\u77e5\u9053\u4e0b\u6807\u8bbf\u95ee\u4e0d\u4f1a\u8d8a\u754c\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528\u5b83\u3002\n@cython.wraparound(False) \u6d88\u9664\u4e86\u76f8\u5bf9\u6570\u7ec4\u5c3e\u90e8\u7684\u8d1f\u6570\u4e0b\u6807\u7684\u5904\u7406\uff08\u7c7b\u4f3cPython\u5217\u8868\uff09\u3002\n\u5f15\u5165\u8fd9\u4e24\u4e2a\u88c5\u9970\u5668\u53ef\u4ee5\u6781\u5927\u7684\u63d0\u5347\u6027\u80fd\uff08\u6d4b\u8bd5\u8fd9\u4e2a\u4f8b\u5b50\u7684\u65f6\u5019\u5927\u6982\u5feb\u4e862.5\u500d\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4efb\u4f55\u65f6\u5019\u5904\u7406\u6570\u7ec4\u65f6\uff0c\u7814\u7a76\u5e76\u6539\u5584\u5e95\u5c42\u7b97\u6cd5\u540c\u6837\u53ef\u4ee5\u6781\u5927\u7684\u63d0\u793a\u6027\u80fd\u3002\n\u4f8b\u5982\uff0c\u8003\u8651\u5bf9 clip() \u51fd\u6570\u7684\u5982\u4e0b\u4fee\u6b63\uff0c\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@cython.boundscheck(False)\n@cython.wraparound(False)\ncpdef clip(double[:] a, double min, double max, double[:] out):\n    if min > max:\n        raise ValueError(\"min must be <= max\")\n    if a.shape[0] != out.shape[0]:\n        raise ValueError(\"input and output arrays must be the same size\")\n    for i in range(a.shape[0]):\n        out[i] = (a[i] if a[i] < max else max) if a[i] > min else min"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9e\u9645\u6d4b\u8bd5\u7ed3\u679c\u662f\uff0c\u8fd9\u4e2a\u7248\u672c\u7684\u4ee3\u7801\u8fd0\u884c\u901f\u5ea6\u8981\u5feb50%\u4ee5\u4e0a\uff082.44\u79d2\u5bf9\u6bd4\u4e4b\u524d\u4f7f\u7528 timeit() \u6d4b\u8bd5\u76843.76\u79d2\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5230\u8fd9\u91cc\u4e3a\u6b62\uff0c\u4f60\u53ef\u80fd\u60f3\u77e5\u9053\u8fd9\u79cd\u4ee3\u7801\u600e\u4e48\u80fd\u8ddf\u624b\u5199C\u8bed\u8a00PK\u5462\uff1f\n\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u5199\u4e86\u5982\u4e0b\u7684C\u51fd\u6570\u5e76\u4f7f\u7528\u524d\u9762\u51e0\u8282\u7684\u6280\u672f\u6765\u624b\u5199\u6269\u5c55\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "void clip(double *a, int n, double min, double max, double *out) {\n  double x;\n  for (; n >= 0; n--, a++, out++) {\n    x = *a;\n\n    *out = x > max ? max : (x < min ? min : x);\n  }\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6211\u4eec\u6ca1\u6709\u5c55\u793a\u8fd9\u4e2a\u7684\u6269\u5c55\u4ee3\u7801\uff0c\u4f46\u662f\u8bd5\u9a8c\u4e4b\u540e\uff0c\u6211\u4eec\u53d1\u73b0\u4e00\u4e2a\u624b\u5199C\u6269\u5c55\u8981\u6bd4\u4f7f\u7528Cython\u7248\u672c\u7684\u6162\u4e86\u5927\u698210%\u3002\n\u6700\u5e95\u4e0b\u7684\u4e00\u884c\u6bd4\u4f60\u60f3\u8c61\u7684\u8fd0\u884c\u7684\u5feb\u5f88\u591a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u4ee5\u5bf9\u5b9e\u4f8b\u4ee3\u7801\u6784\u5efa\u591a\u4e2a\u6269\u5c55\u3002\n\u5bf9\u4e8e\u67d0\u4e9b\u6570\u7ec4\u64cd\u4f5c\uff0c\u6700\u597d\u8981\u91ca\u653eGIL\uff0c\u8fd9\u6837\u591a\u4e2a\u7ebf\u7a0b\u80fd\u5e76\u884c\u8fd0\u884c\u3002\n\u8981\u8fd9\u6837\u505a\u7684\u8bdd\uff0c\u9700\u8981\u4fee\u6539\u4ee3\u7801\uff0c\u4f7f\u7528 with nogil: \u8bed\u53e5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@cython.boundscheck(False)\n@cython.wraparound(False)\ncpdef clip(double[:] a, double min, double max, double[:] out):\n    if min > max:\n        raise ValueError(\"min must be <= max\")\n    if a.shape[0] != out.shape[0]:\n        raise ValueError(\"input and output arrays must be the same size\")\n    with nogil:\n        for i in range(a.shape[0]):\n            out[i] = (a[i] if a[i] < max else max) if a[i] > min else min"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5199\u4e00\u4e2a\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u7684\u7248\u672c\uff0c\u4e0b\u9762\u662f\u53ef\u4ee5\u53c2\u8003\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@cython.boundscheck(False)\n@cython.wraparound(False)\ncpdef clip2d(double[:,:] a, double min, double max, double[:,:] out):\n    if min > max:\n        raise ValueError(\"min must be <= max\")\n    for n in range(a.ndim):\n        if a.shape[n] != out.shape[n]:\n            raise TypeError(\"a and out have different shapes\")\n    for i in range(a.shape[0]):\n        for j in range(a.shape[1]):\n            if a[i,j] < min:\n                out[i,j] = min\n            elif a[i,j] > max:\n                out[i,j] = max\n            else:\n                out[i,j] = a[i,j]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5e0c\u671b\u8bfb\u8005\u4e0d\u8981\u5fd8\u4e86\u672c\u8282\u6240\u6709\u4ee3\u7801\u90fd\u4e0d\u4f1a\u7ed1\u5b9a\u5230\u67d0\u4e2a\u7279\u5b9a\u6570\u7ec4\u5e93\uff08\u6bd4\u5982NumPy\uff09\u4e0a\u9762\u3002\n\u8fd9\u6837\u4ee3\u7801\u5c31\u66f4\u6709\u7075\u6d3b\u6027\u3002\n\u4e0d\u8fc7\uff0c\u8981\u6ce8\u610f\u7684\u662f\u5982\u679c\u5904\u7406\u6570\u7ec4\u8981\u6d89\u53ca\u5230\u591a\u7ef4\u6570\u7ec4\u3001\u5207\u7247\u3001\u504f\u79fb\u548c\u5176\u4ed6\u56e0\u7d20\u7684\u65f6\u5019\u60c5\u51b5\u4f1a\u53d8\u5f97\u590d\u6742\u8d77\u6765\u3002\n\u8fd9\u4e9b\u5185\u5bb9\u5df2\u7ecf\u8d85\u51fa\u672c\u8282\u8303\u56f4\uff0c\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u8003 PEP 3118 \uff0c\n\u540c\u65f6 Cython\u6587\u6863\u4e2d\u5173\u4e8e\u201c\u7c7b\u578b\u5185\u5b58\u89c6\u56fe\u201d\n\u7bc7\u4e5f\u503c\u5f97\u4e00\u8bfb\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}